<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

          <a-col :xs="24" :sm="12">
            <a-form-item label="料站表编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mtableCode', validatorRules.mtableCode]" placeholder="请输入料站表编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="料站表名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mtableName', validatorRules.mtableName]" placeholder="请输入料站表名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="加工面别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['processFace', validatorRules.processFace]" :trigger-change="true" dictCode="process_face" placeholder="请选择加工面别"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="机种料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['machineCode', validatorRules.machineCode]" placeholder="请输入机种料号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="机种名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['machineName']" placeholder="请输入机种名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="机种规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['machineGague']" placeholder="请输入机种规格"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="贴片速率" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pieceRate', validatorRules.pieceRate]" placeholder="请输入贴片速率"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="贴片点数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['piecePointnum']" placeholder="请输入贴片点数"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="有效标志" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['validToken']" :trigger-change="true" dictCode="yn" placeholder="请选择有效标志"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="轨道" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['track', validatorRules.track]" :trigger-change="true" dictCode="smt_track" placeholder="请选择轨道"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="主数据—料站表明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesMaterialtableItemTable.loading"
            :columns="mesMaterialtableItemTable.columns"
            :dataSource="mesMaterialtableItemTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'MesChiefdataMaterialtableModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          mtableCode: {
            rules: [
              { required: true, message: '请输入料站表编码!'},
            ]
          },
          mtableName: {
            rules: [
              { required: true, message: '请输入料站表名称!'},
            ]
          },
          processFace: {
            rules: [
              { required: true, message: '请输入加工面别!'},
            ]
          },
          machineCode: {
            rules: [
              { required: true, message: '请输入机种料号!'},
            ]
          },
          pieceRate: {
            rules: [
              { required: true, message: '请输入贴片速率!'},
            ]
          },
          track: {
            rules: [
              { required: true, message: '请输入轨道!'},
            ]
          },
        },
        refKeys: ['mesMaterialtableItem', ],
        tableKeys:['mesMaterialtableItem', ],
        activeKey: 'mesMaterialtableItem',
        // 主数据—料站表明细
        mesMaterialtableItemTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '贴片机型',
              key: 'pieceMachine',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '程式名称',
              key: 'formulaName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '设备序号',
              key: 'deviceRank',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'TABLE',
              key: 'mtable',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '料站',
              key: 'materielStation',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '通道',
              key: 'passage',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料料号',
              key: 'materielCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料名称',
              key: 'materielName',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '物料规格',
              key: 'materielGague',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'FEEDER规格',
              key: 'feederGague',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'TRAY盘物料',
              key: 'trayPlate',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '跳过标志',
              key: 'skipToken',
              type: FormTypes.select,
              dictCode:"yn",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '点数',
              key: 'pointNum',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '点位',
              key: 'pointLocation',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: 'BOM范围',
              key: 'bomRange',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/chiefdata/mesChiefdataMaterialtable/add",
          edit: "/chiefdata/mesChiefdataMaterialtable/edit",
          mesMaterialtableItem: {
            list: '/chiefdata/mesChiefdataMaterialtable/queryMesMaterialtableItemByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'mtableCode','mtableName','processFace','machineCode','machineName','machineGague','pieceRate','piecePointnum','validToken','track','notes')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesMaterialtableItem.list, params, this.mesMaterialtableItemTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          mesMaterialtableItemList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'mtableCode','mtableName','processFace','machineCode','machineName','machineGague','pieceRate','piecePointnum','validToken','track','notes'))
     },

    }
  }
</script>

<style scoped>
</style>